package com.itheima.service.Impl;

import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.config.RedisConfiguration;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSetTingMapper;
import com.itheima.mapper.VipMapper;
import com.itheima.pojo.Dto.OrderDto;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderPojo;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Vip;
import com.itheima.pojo.Vo.OrderVo;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderSetTingMapper orderSetTingMapper;
    @Autowired
    private VipMapper vipMapper;

    /**
     * 提交预约信息
     * @param orderDto
     */
    @Override
    public void submitOrder(OrderDto orderDto) {
        OrderPojo selectOrderPojo = new OrderPojo();
        BeanUtils.copyProperties(orderDto,selectOrderPojo);
      //  2. 校验用户输入的验证码是否正确。
        String inputCode = orderDto.getValidateCode();
        String key = RedisMessageConstant.SENDTYPE_ORDER+orderDto.getTelephone();
       String redisCode = (String) redisTemplate.opsForValue().get( key);
        if(redisCode == null || !redisCode.equals(inputCode)){
            throw new RuntimeException("验证码输入错误！");
        }
      //  3. 预约日期的预约人数没有设置的话不能预约。
       OrderSetting orderSetting = orderSetTingMapper.getOrderSettingByOrderDate(orderDto.getOrderDate());
        if(orderSetting == null){
            throw new RuntimeException("预约日期的预约人数没有设置！");
        }
       // 4. 预约日期是否已经约满，如果已经约满则无法预约。
        if(orderSetting.getReservations() >= orderSetting.getNumber()){
            throw new RuntimeException("预约日期已经约满！");
        }

       // 5. 不能重复预约（同一个用户在同一天预约了同一个套餐）。
      OrderPojo orderPojo = orderMapper.selectByIdCardAndOrderDate(orderDto.getIdCard(),orderDto.getOrderDate());
        if(orderPojo != null){
            throw new RuntimeException("不能重复预约！");
        }
       // 6. 当前用户不是会员，需要自动完成注册。
       Vip vip = vipMapper.selectByTelephone(orderDto.getTelephone());
        if( vip== null){
            vipMapper.add(orderDto.getTelephone());
        }
      //  7. 更新已预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSetTingMapper.updateByReservations(orderSetting.getReservations(),orderDto.getOrderDate());
        // 8. 添加预约信息
        selectOrderPojo.setMemberId(vip.getId());
        selectOrderPojo.setOrderStatus(Order.ORDERSTATUS_NO);
        selectOrderPojo.setOrderType(Order.ORDERTYPE_TELEPHONE);
        selectOrderPojo.setMarry(2);//默认没结婚
        selectOrderPojo.setCreateTime(LocalDateTime.now());
        orderMapper.add(selectOrderPojo);
    }


    /**
     * 根据id查询预约信息
     * @param id
     * @return
     */
    @Override
    public OrderVo getById(Integer id) {
        OrderPojo orderPojo = orderMapper.getById(id);
        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(orderPojo,orderVo);
        return orderVo;
    }
}
